/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.processor.business;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.component.gateway.dianlan.annotations.DianlanProtocolParse;
import info.peigen.hotpot.component.gateway.dianlan.facade.command.order.DianlanStubChargeResponseOrder;
import info.peigen.hotpot.component.gateway.dianlan.processor.AbstractDianlanProcessor;
import info.peigen.hotpot.component.gateway.dianlan.protocol.Dianlan;
import info.peigen.hotpot.component.gateway.dianlan.protocol.active.DianlanRemoteStartChargeResponse;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.StateMachineEnum;
import org.smartboot.socket.transport.AioSession;

/**
 * <b>(DianlanHeartProcessor)</b>
 *
 * @author czj
 * @version 1.0.0
 * @since 2022/11/17
 */
@Component("dianlanChargeResponseProcessor")
@Slf4j
public class DianlanChargeResponseProcessor<T> extends AbstractDianlanProcessor implements MessageProcessor<T> {

    @Override
    public void process(AioSession session, Object obj) {
        Dianlan                          dianlan  = (Dianlan) obj;
        DianlanRemoteStartChargeResponse response = DianlanProtocolParse.deserialization(dianlan.getData(), DianlanRemoteStartChargeResponse.class);
        if (StrUtil.equals(response.getChargingPortNo(), IGNORE_PORT)) {
            ignorePort(dianlan);
            return;
        }
        log.info("Data:" + JSON.toJSONString(response));
        DianlanStubChargeResponseOrder order = DianlanStubChargeResponseOrder.builder()
                .stubNo(response.getStubNo())
                .transNo(response.getTransNo())
                .authSign(response.getAuthSign())
                .failReason(response.getFailReason())
                .chargingPortNo(response.getChargingPortNo())
                .build();
        dianlanServiceFacade.chargeResponse(order);
    }

    @Override
    public void stateEvent(AioSession session, StateMachineEnum stateMachineEnum, Throwable throwable) {
        MessageProcessor.super.stateEvent(session, stateMachineEnum, throwable);
    }
}